package com.mobile.challenge.ui.report

import android.content.Intent
import android.os.Bundle
import android.view.View
import com.google.android.material.tabs.TabLayoutMediator
import com.mobile.challenge.R
import com.mobile.challenge.databinding.FragmentReportBinding
import com.mobile.challenge.ui.SeatChooseActivity
import com.mobile.challenge.ui.report.adapter.ReportViewPagerAdapter
import com.mobile.challenge.ui.report.model.PagerFragmentModel
import com.mobile.challenge.ui.report.utils.ReportPagerUtils
import com.mobile.challenge.user.UserHelper
import com.mobile.frame.mvvmframe.base.BaseFragment

/**
 * @Author Touge
 * @Date 2020/9/15 22:29
 * @Description
 */
class ReportFragment : BaseFragment<ReportViewModel, FragmentReportBinding>() {

    private var pagerList = ArrayList<PagerFragmentModel>(0)
    private lateinit var adapter: ReportViewPagerAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        adapter = ReportViewPagerAdapter(this)
    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_report
    }

    override fun initData(savedInstanceState: Bundle?) {
        // 是员工就不显示 TabLayout
        if (UserHelper.getInstance().isStaff()) {
            mBinding.tabLayout.visibility = View.GONE
        } else {
            mBinding.tabLayout.visibility = View.VISIBLE
        }

        mBinding.viewPager.isUserInputEnabled = false
        mBinding.viewPager.adapter = adapter
        pagerList = ReportPagerUtils.getReportFragmentPager()
        adapter.fragmentList = pagerList

        TabLayoutMediator(mBinding.tabLayout, mBinding.viewPager,
                TabLayoutMediator.TabConfigurationStrategy { tab, position ->
                    tab.text = pagerList[position].name
                }
        ).attach()

        if (UserHelper.getInstance().isManager()) {
            mBinding.floorReport.visibility = View.VISIBLE
            mBinding.floorReport.setOnClickListener {
                val intent = Intent(requireContext(), SeatChooseActivity::class.java)
                intent.putExtra("only_look", true)
                startActivity(intent)
            }
        } else {
            mBinding.floorReport.visibility = View.GONE
        }

    }
}